package com.departmentstore.controller;

import com.departmentstore.entity.Employee;
import com.departmentstore.service.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/employees")
@Tag(name = "员工管理", description = "员工的增删改查接口")
public class EmployeeController {

    private final EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @PostMapping
    @Operation(summary = "创建员工", description = "创建一个新的员工")
    public ResponseEntity<Employee> createEmployee(@Valid @RequestBody Employee employee) {
        return ResponseEntity.ok(employeeService.createEmployee(employee));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新员工", description = "根据ID更新员工信息")
    public ResponseEntity<Employee> updateEmployee(
            @Parameter(description = "员工ID") @PathVariable String id,
            @Valid @RequestBody Employee employee) {
        employee.setEmployeeId(id);
        return ResponseEntity.ok(employeeService.updateEmployee(employee));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除员工", description = "根据ID删除员工")
    public ResponseEntity<Void> deleteEmployee(
            @Parameter(description = "员工ID") @PathVariable String id) {
        employeeService.deleteEmployee(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取员工", description = "根据ID获取员工信息")
    public ResponseEntity<Employee> getEmployee(
            @Parameter(description = "员工ID") @PathVariable String id) {
        return ResponseEntity.ok(employeeService.getEmployeeById(id));
    }

    @GetMapping
    @Operation(summary = "获取所有员工", description = "获取所有员工的列表")
    public ResponseEntity<List<Employee>> getAllEmployees() {
        return ResponseEntity.ok(employeeService.getAllEmployees());
    }

    @GetMapping("/name/{name}")
    @Operation(summary = "根据姓名查找员工", description = "根据员工姓名查找员工信息")
    public ResponseEntity<List<Employee>> getEmployeesByName(
            @Parameter(description = "员工姓名") @PathVariable String name) {
        return ResponseEntity.ok(employeeService.getEmployeesByName(name));
    }

    @GetMapping("/department/{departmentId}")
    @Operation(summary = "根据部门ID查找员工", description = "根据部门ID查找该部门的所有员工")
    public ResponseEntity<List<Employee>> getEmployeesByDepartmentId(
            @Parameter(description = "部门ID") @PathVariable String departmentId) {
        return ResponseEntity.ok(employeeService.getEmployeesByDepartmentId(departmentId));
    }
} 